package com.chixing.service.impl;

import com.chixing.mapper.CustomerMapper;
import com.chixing.pojo.Customer;
import com.chixing.pojo.CustomerExample;
import com.chixing.service.RegisterService;
import com.chixing.util.AESUtil;
import com.chixing.util.RSAUtils;
import com.chixing.util.ServerResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.security.KeyPair;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.util.Base64;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 *
 * @Author: wtl
 * @Date: 2021/10/17/3:21
 * @Description:
 */
@Service
public class RegisterServiceImpl implements RegisterService {

    private static KeyPair keyPair;
    @Autowired
    private CustomerMapper customerMapper;

    @Override
    public ServerResponse save(Long custTelno, String custPassword) {
        System.out.println("注册密码："+custPassword);

        String encrypted = custPassword;
        String privateKey = getPrivateKey();

        try {
            System.out.println("RSA解密中.....");
            String decrypted = RSAUtils.privateDecrypt(encrypted, RSAUtils.getPrivateKey(privateKey));

            System.out.println("RSA解密后：" + decrypted);
            String encrypt =  AESUtil.encrypt(decrypted);

            System.out.println("AES加密后：" + encrypt);
            custPassword = encrypt;

        } catch (Exception e) {
            e.printStackTrace();
        }

        System.out.println("consumerPassword:"+custPassword);
        Customer customer = new Customer();

        customer.setCustTelno(custTelno);
        customer.setCustPassword(custPassword);
        int rows = customerMapper.insert(customer);
        if(rows>0)
            return ServerResponse.saveSucccess(rows);
        return ServerResponse.saveFail(null);
    }
    @Override
    public ServerResponse getCustTelno(Long custTelno) {
        CustomerExample example = new CustomerExample();
        example.createCriteria().andCustTelnoEqualTo(custTelno);


        List<Customer> consumerList = customerMapper.selectByExample(example);
        System.out.println("consumerList.size(): "+consumerList.size());
        if (consumerList.size() > 0) {
            return ServerResponse.getSucccess("existence");
        }
        return ServerResponse.getFail("non-existent");
//        return null;
    }


    @Override
    public ServerResponse getPublicKey() {
        try {
            keyPair = RSAUtils.genKeyPair(1024);
            PublicKey publicKey=keyPair.getPublic();
            System.out.println("公钥：");
            String pubKey= new String(Base64.getEncoder().encode(publicKey.getEncoded()));
            System.out.println(pubKey);
            return ServerResponse.getSucccess(pubKey);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ServerResponse.getFail(null);
    }


    @Override
    public String getPrivateKey() {
        try {
            PrivateKey privateKey=keyPair.getPrivate();
            System.out.println("get私钥：");
            String priKey = new String(Base64.getEncoder().encode(privateKey.getEncoded()));
            System.out.println(priKey);
            return priKey;

        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }






    }

